package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.IFigure;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.execution.Model;
import edu.pku.sei.transformation.editor.views.figures.TagedElementFigure;

public class ModelEditpart extends NodeEditpart {

	public ModelEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected IFigure createFigure() {
		TagedElementFigure figure = new TagedElementFigure();
		Model data = getTypedModel();
		figure.setName(data.toString());
		figure.setTag("<<Model>>");
		figure.setContent(data.getFile());
		return figure;
	}

	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		Model data = getTypedModel();
		TagedElementFigure figure = (TagedElementFigure) getFigure();
		if (ModelPackage.MODEL_METAMODEL.equals(propertyName)
				|| ModelPackage.MODEL_NAME.equals(propertyName)) {
			figure.setName(data.toString());

		} else if (ModelPackage.MODEL_FILE.equals(propertyName)){
			figure.setContent(data.getFile());
		} else {
			super.handlePropertyChanged(event);
		}
	}

	private Model getTypedModel() {
		return (Model) getNode().getModelElement();
	}
}
